/*
 * interrupt.c
 *
 *  Created on: 23/dic/2013
 *      Author: robotis
 */

#include <msp430x552x.h>

volatile unsigned char receiveFlag = 0;
volatile unsigned char RXbuff[8], W_PTR = 0, R_PTR = 0;
/// service interrupt routine

// Echo back RXed character, confirm TX buffer is ready first
// usata nella seriale di collegamento al pc
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
  switch(__even_in_range(UCA1IV,4))
  {
  case 0:break;                             // Vector 0 - no interrupt
  case 2:                                   // Vector 2 - RXIFG

	  //receiveFlag = 1;
	  while (!(UCA1IFG & UCTXIFG));
	  UCA1RXBUF = UCA1RXBUF;
    break;
  case 4:break;                             // Vector 4 - TXIFG
  default: break;
  }
}

///
/// riceve dati dalla scheda con sensore di distanza.
///
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
  switch(__even_in_range(UCA0IV,4))
  {
  case 0:break;                             // Vector 0 - no interrupt
  case 2:                                   // Vector 2 - RXIFG

	  //receiveFlag = 1;
	  RXbuff[W_PTR++] = UCA0RXBUF;
	  W_PTR &= 7;

    break;
  case 4:break;                             // Vector 4 - TXIFG
  default: break;
  }
}


volatile unsigned int contatore = 0;


volatile unsigned int tempoLocale = 0, invio = 0;
// Timer1 A0 interrupt service routine
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{

	//P1OUT ^= 1;
	contatore++;
	if ((contatore & 15) == 0)
		invio = 1;
	/// solo per scopi di debug e test
	tempoLocale++;
	if (tempoLocale == 50){
		P1OUT ^= 1;
		tempoLocale = 0;
		//TX_PTR++;
		//TX_PTR &= 7;
	}

}

